<template>
  <a-modal
    title="编辑技能名称"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :ok-button-props="{ props: { disabled: !isValid } }"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-item
        label="技能名称"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input
          name="skillName"
          v-model="skillName"
        />
      </a-form-item>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  name: 'UpdateSkillModal',
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      visible: false,
      confirmLoading: false,
      category: { skillName: '' },
      skillName: ''
    }
  },
  computed: {
    isValid () {
      return (this.skillName !== this.category.skillName) && (this.skillName !== '')
    }
  },
  methods: {
    show (category) {
      this.visible = true
      this.category = category
      this.skillName = category.skillName
    },
    handleSubmit () {
      const { skillName, category } = this
      this.confirmLoading = true
      if (this.isValid) {
        this.$emit('updateSkill', Object.assign(category, { skillName }), () => {
          this.confirmLoading = false
          this.category = { skillName: '' }
          this.handleCancel()
        })
      } else {
        this.confirmLoading = false
      }
    },
    handleCancel () {
      this.visible = false
    }
  }
}
</script>
